package com.jxtti.controller;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.SolicitPublic;
import com.jxtti.model.User;
import com.jxtti.service.SolicitPublicService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.ExceptionUtil;
import com.jxtti.utils.StringUtil;
/**
 * 民意征集
 * bff 
 */ 
@Controller
@RequestMapping("/solicitPublicController") 
public class SolicitPublicController {
	
	private final static Logger log= Logger.getLogger(SolicitPublicController.class);
	
	@InitBinder  
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	
	@Autowired
	private SolicitPublicService  solicitPublicService; 
	
		
	@RequestMapping(value = "/listInterface")
	@ResponseBody
	public ModelAndView listInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/SolicitPublic/SolicitList.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}	
	
	@RequestMapping(value="/list", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object>  SolicitPubliclist(@RequestParam Map<String, Object> data, SolicitPublic solicitPublic, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		Map<String, Object> result = new HashMap<String, Object>();	
		
		data.put("is_deleted","0");
		//判断是否有时间 ，默认是今天
		if ((data.get("startDate")==null||data.get("startDate").equals(""))&&(data.get("endDate")==null||data.get("endDate").equals(""))) {						
			SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");//完整的时间			
			Calendar c = Calendar.getInstance();  
		    c.add(Calendar.DATE, - 30); //一个月					       
	        Date monday = c.getTime();
		    String preMonday = sdf.format(monday);		        		
			data.put("startDate", preMonday);			
		}
		
		PageInfo<SolicitPublic> page = solicitPublicService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/SolicitPublic/SolicitEdit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SolicitPublic  solicitPublic, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			User user = AuthUtil.getUserInfo(request);		
			solicitPublic.setCreateUserID(user.getId().toString());
			
			if(solicitPublic.getContents()!=null && !solicitPublic.getContents().equals("")){				
				solicitPublic.setContents(StringUtil.replaceEnter(solicitPublic.getContents())); // 去除换行符				
			}			
			int exeCount = solicitPublicService.add(solicitPublic);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SolicitPublic  solicitPublic,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			
			solicitPublic = solicitPublicService.queryById(solicitPublic);
			if(solicitPublic.getContents()!=null && !solicitPublic.getContents().equals("")){				
				solicitPublic.setContents(StringUtil.replaceEnter(solicitPublic.getContents())); // 去除换行符				
			}
			data.put("solicitPublic", solicitPublic);
			mav.addObject("data", data);			
			mav.setViewName("/pages/Admin/Site/SolicitPublic/SolicitEdit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	// 意见采纳说明
	@RequestMapping(value = "/remarkInterface")
	@ResponseBody
	public ModelAndView remarkInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SolicitPublic  solicitPublic,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			solicitPublic = solicitPublicService.queryById(solicitPublic);
			if(solicitPublic.getContents()!=null && !solicitPublic.getContents().equals("")){				
				solicitPublic.setContents(StringUtil.replaceEnter(solicitPublic.getContents())); // 去除换行符				
			}
			data.put("solicitPublic", solicitPublic);
			mav.addObject("data", data);					
			mav.setViewName("/pages/Admin/Site/SolicitPublic/SolicitRemark.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SolicitPublic  solicitPublic, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 修改			
			int exeCount;
			if(data.get("type")!=null&&data.get("type").equals("adopteds")){ //上移下移
				SolicitPublic	tsolicit=solicitPublicService.queryById(solicitPublic);				
				tsolicit.setAdopteds(solicitPublic.getAdopteds());				
				exeCount = solicitPublicService.update(tsolicit);
			}else{
				exeCount = solicitPublicService.update(solicitPublic);
			}
			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
		
/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			 HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				SolicitPublic  solicitPublic=new SolicitPublic();
				solicitPublic.setId(Integer.parseInt(id));	
				solicitPublic=solicitPublicService.queryById(solicitPublic);
				solicitPublic.setIs_deleted(1);
				int exeCount = solicitPublicService.update(solicitPublic);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}				
			}	
			
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
